//
//  WarSettingLayer.cpp
//  DiceWar
//
//  Created by xiaofeng on 27/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include "WarSettingLayer.h"
#include "GameUtil.h"
#include "SpriteUtil.h"
#include "GameMaster.h"
#include "StringUtil.h"
#include "SceneManager.h"

USING_NS_CC;

namespace  
{
    const char* kBackgroundImageName = "mainpage.png";
    const char* kDefaultLable = "Default";
    
    const char* kPeopleVsPeopleModeName = "People VS People";
    const char* kPeopleVsAIModeName = "People VS Comp";
    
    const int kBackgroundDepth = 0;
    const int kUserinterfaceDepth = 1;
    const ccColor3B kFontColor = {0, 0, 255};
}

bool WarSettingLayer::init()
{
    if (!CCLayer::init()) return false;
    if (!initBackground()) return false;
    if (!initUserinterface()) return false;
    if (!initDisplay()) return false;
    
    return true;
}

bool WarSettingLayer::initBackground()
{
    const CCSize kWinSize = GameUtil::getWindowSize();
    CCSprite* pSprite = SpriteUtil::createSprite(kBackgroundImageName, ccp(kWinSize.width/2.0f, kWinSize.height/2.0f), CCSize(1.0f, 1.0f));
    if (!pSprite) return false;
    addChild(pSprite, kBackgroundDepth);
    
    return true;
}

bool WarSettingLayer::initUserinterface()
{
    CCMenuItemImage* pChangeModeButton = CCMenuItemImage::itemWithNormalImage("changeModeButton.png", "changeModeButtonDown.png",  this, menu_selector(WarSettingLayer::onChangeModeButtonDown));
    if (!pChangeModeButton) return false;
    GameUtil::setRelativeToScreenPosition(pChangeModeButton, ccp(-0.2f, 0.3f));
    pChangeModeButton->setScale(0.3f);
    
    CCMenuItemImage* pChangeNumPlayersButton = CCMenuItemImage::itemWithNormalImage("changeNumPlayerButton.png", "changeNumPlayerButtonDown.png", this, menu_selector(WarSettingLayer::onChangeNumPlayersButtonDown));
    if (!pChangeModeButton) return false;
    GameUtil::setRelativeToScreenPosition(pChangeNumPlayersButton, ccp(-0.2f, 0.0f));
    pChangeNumPlayersButton->setScale(0.3f);
    
    CCMenuItemImage* pStartGameButton = CCMenuItemImage::itemWithNormalImage("playButton.png", "playButtonDown.png", this, menu_selector(WarSettingLayer::onNextButtonDown));
    if (!pStartGameButton) return false;
    GameUtil::setRelativeToScreenPosition(pStartGameButton, ccp(-0.2f, -0.3f));
    pStartGameButton->setScale(0.3f);
    
    CCMenu* pMenu = CCMenu::menuWithItems(pChangeModeButton, pChangeNumPlayersButton, pStartGameButton, NULL);
    if (!pMenu) return false;
    addChild(pMenu, kUserinterfaceDepth);
    
    return true;
}

bool WarSettingLayer::initDisplay()
{
    m_pGameModeLabel = CCMenuItemFont::itemWithString(kDefaultLable);
    if (!m_pGameModeLabel) return false;
    GameUtil::setRelativeToScreenPosition(m_pGameModeLabel, ccp(0.7f, 0.8f));
    m_pGameModeLabel->setColor(kFontColor);
    addChild(m_pGameModeLabel, kUserinterfaceDepth);
    
    m_pNumPlayersLabel = CCMenuItemFont::itemWithString(kDefaultLable);
    if (!m_pNumPlayersLabel) return false;
    GameUtil::setRelativeToScreenPosition(m_pNumPlayersLabel, ccp(0.7f, 0.5f));
    m_pNumPlayersLabel->setColor(kFontColor);
    addChild(m_pNumPlayersLabel, kUserinterfaceDepth);
    
    updateModeLabel();
    updateNumPlayersLabel();
        
    return true;
}

void WarSettingLayer::onChangeModeButtonDown(cocos2d::CCObject* pSender)
{
    GameMaster& gameMaster = GameMaster::instance();
    if (gameMaster.isPeopleVsAI()) gameMaster.setGameMode(GameMaster::kPeopleVsPeople);
    else if (gameMaster.isPeopleVsPeople()) gameMaster.setGameMode(GameMaster::kPeopleVsAI);
    updateModeLabel();
}

void WarSettingLayer::onChangeNumPlayersButtonDown(cocos2d::CCObject* pSender)
{
    GameMaster& gameMaster = GameMaster::instance();
    int numPlayers = gameMaster.getNumPlayers();
    const int kMaxNumPlayers = gameMaster.getMaxNumPlayers();
    
    if (numPlayers >= kMaxNumPlayers) numPlayers = 2;
    else numPlayers++;
    
    gameMaster.setNumPlayers(numPlayers);
    updateNumPlayersLabel();
}

void WarSettingLayer::onNextButtonDown(cocos2d::CCObject* pSender)
{
    SceneManager::sharedSceneManager().switchToScene(SceneManager::kMapSelectionScene);
}

void WarSettingLayer::updateModeLabel()
{
    GameMaster& gameMaster = GameMaster::instance();
    if (gameMaster.isPeopleVsPeople()) m_pGameModeLabel->setString(kPeopleVsPeopleModeName);
    else if (gameMaster.isPeopleVsAI()) m_pGameModeLabel->setString(kPeopleVsAIModeName);
}

void WarSettingLayer::updateNumPlayersLabel()
{
    const int kNumPlayers = GameMaster::instance().getNumPlayers();
    m_pNumPlayersLabel->setString(StringUtil::ToString(kNumPlayers).c_str());
}
